package com.sy.controller;

import com.github.pagehelper.Page;
import com.sy.auto.token.TokenTemplate;
import com.sy.bean.User;
import com.sy.bean.params.UserQuery;
import com.sy.common.bean.PageBean;
import com.sy.common.bean.ResultBean;
import com.sy.common.controller.BaseController;
import com.sy.common.utils.BeanOfMap;
import com.sy.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/api")
@Api(tags = "用户管理")
public class UserController extends BaseController {
    @Autowired
    TokenTemplate tokenTemplate;
    @Autowired
    UserService userService;
    @PostMapping("/user")
    @ApiOperation("用户查询")
    public ResultBean<PageBean<User>> query(@RequestBody UserQuery userQuery){
        PageBean bean = userService.query(BeanOfMap.bom(userQuery));
        return success("用户列表",bean);
    }
    @PostMapping("/user/add")
    @ApiOperation("新增用户")
    public ResultBean save(@Validated @RequestBody User user,BindingResult result){
        validate(result);
        int num = userService.save(user);
        return num==1?success("增加成功"):fail("增加失败");
    }
    @DeleteMapping("/user")
    @ApiOperation("删除用户")
    @ApiImplicitParam(name = "id",value = "用户id",required = true,dataTypeClass = Integer.class)
    public ResultBean delete(int id){
        int num = userService.delete(id);
        return num==1?success("删除成功"):fail("删除失败");
    }
}
